/*
Filename: UCDeleteUser.java
Precondition: User enter a valid username and password
Post-condition: User can delete from the system.
Main success Scenario:
User selects to delete
1. User enters username and password
2. User clicks ‘delete’ button
3. System retrieves username and password
4. System verifies the delete information
5. System deletes the user information
6. System deletes user from the list of online users
7. System displays message “User Deleted”
8. System updates the main view.
2a. Extensions: System detects invalid delete:
1. System display “invalid user delete” message
 */
package ucm;

import model.UsersModel;
import model.DatabaseModel;
import view.DeleteView;

import javax.swing.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
public class UCDeleteUser implements UCController {

    private UsersModel user;

    public UCDeleteUser() {
        user = new UsersModel("", "", "", "", "");
    }

    public UCDeleteUser(UsersModel user) {
        // System retrieves username and password
        this.user = user;
    }

    public UCDeleteUser(DeleteView deleteView) {
        // System retrieves username and password
        this.user = deleteView.getUserModel();
    }

    // STEP 1 - User Selects Delete
    public boolean run() {
        boolean deleteSuccessful = false;
        // STEP 2 - System retrieves username and password
        // This step is integrated into the constructor or
        // may be specified by using run(UserModel)
        // STEP 3 - System validates the username and password
        if (validUser()) {
            //Step 4 - System deletes user from the database
            if (deleteUser()) {
                //Step 5 - System displays 'user deleted' message
                JOptionPane.showMessageDialog(new JFrame(),
                        "User Deleted Successfully",
                        "Success",
                        JOptionPane.INFORMATION_MESSAGE);
                deleteSuccessful = true;
                new UCDispalyTicketsaler();
            } else {
                JOptionPane.showMessageDialog(new JFrame(),
                        "Deletion failed for unknown reason",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);
                new UCDisplayDeleteUser();
            }
        } else {
            // STEP 2a - System displays error message
            JOptionPane.showMessageDialog(new JFrame(),
                    "Invalid Username or Password. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            new UCDisplayDeleteUser();
        }
        return deleteSuccessful;
    }

    // STEP 1 - User Selects Delete
    public void run(UsersModel user) {
        // STEP 2 - System retrieves username and password
        this.user = user;
        // STEP 3 onwards
        run();
    }

    private boolean validUser() {
        boolean correct = false;
        String username = user.getUsername();
        String password = user.getPassword();

        Connection con = DatabaseModel.getInstance().getConnection();
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);

            ResultSet res = stmt.executeQuery("SELECT * FROM users WHERE " +
                    "uname = '" + username + "'");

            correct = res.first() && username.equals(res.getString("uname"))
                    && password.equals(res.getString("upassword"));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return correct;
    }

    private boolean deleteUser() {
        boolean delete = false;
        String username = user.getUsername();
        Connection con = DatabaseModel.getInstance().getConnection();
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);

            stmt.executeUpdate("DELETE FROM users WHERE uname = '"
                    + username + "'");

            ResultSet res = stmt.executeQuery("SELECT * FROM users " +
                    "WHERE uname = '" + username + "'");

            delete = !(res.first() && username.equals(res.getString("uname")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return delete;
    }
}


